import commonConstants from "@/common/commonConstants";
import commonMethod from "@/common/commonMethod";
import { CallbackProperty, Cartesian2, Cartesian3, Color, ConstantProperty, defined, Math as CesiumMath, EllipsoidGeodesic, HeightReference, LabelStyle, PolygonGraphics, PolygonHierarchy, PolylineDashMaterialProperty, ScreenSpaceEventHandler, ScreenSpaceEventType, EllipsoidGeometry, EllipsoidOutlineGeometry } from "cesium";
import xp from "./algorithm";
import Marker from "../marker/Marker";
import EllipseGeometryLibrary from "@/common/ellipseGeometryLibrary";
import RightMenu from "@/common/rightMenu";

/**
 * 细直箭头
 */
export class PlotFrankArrowMarker{
    constructor(v){
        this.curViewer = v;
        this.scene = v.scene;
        this.clock = v.clock;
        this.canvas = v.scene.canvas;
        this.camera = v.scene.camera;
        this.ellipsoid = v.scene.globe.ellipsoid;

        this.material = Color.fromCssColorString('#ff0').withAlpha(0.5);
        this.outlineMaterial = new PolylineDashMaterialProperty({
            dashLength: 16,
            color: Color.fromCssColorString('#f00').withAlpha(0.7)
        });
        this.outlineWidth = 2;
        this.fill = true;
        this.outline = true;
        this.tempControlPoint = [];

        this.pickEntity = undefined;
        this.pickContrlPoint = undefined;
        this.positions = undefined;
        this.drawHandler = undefined;
        this.params = undefined;
        this.tempEntity = undefined;
        this.finishCallback = undefined;
        this.monitorManager = undefined;
        this.firstPos = undefined;
    
        this.isOneTargger = false;
        this.monitorKey =  commonConstants.monitorKey.line.plotFrankArrow,
        this.rightmenu = undefined;
       
        this.isActive = false;
        this.isEdit = false;
    }

    setParams(params){
        let _this = this;
        _this.params = params;
    }
    setFinishCallback(callbackFunction){
        let _this = this;
        _this.finishCallback = callbackFunction;
    }
     /**
     * 
     * @param {MonitorManager} manager 
     */
     setManager(manager){
        const _this = this;
        _this.monitorManager = manager;
    }

    setActive(bool){
        let _this = this;
        _this.isActive = bool;
    }
    /**
     * 这是开始标绘普通航迹
     */
    deploy() {
        let _this = this;

        if (!_this.drawHandler) {
            _this.positions = [];
            _this.firstPos = undefined;
            _this.drawHandler = new ScreenSpaceEventHandler(_this.canvas);

            _this.drawHandler.setInputAction(event=>{

                if (!_this.isActive) {
                    return;
                }

                let p = commonMethod.judgeClickPosition(event.position,_this.curViewer);
                if (!p) {
                    return;
                }

               _this.destoryRightMenu();
                let cartesian = _this.scene.globe.pick(p.ray, _this.scene);

                if (!defined(cartesian)) {
                    return;
                }

                if (_this.isEdit) {
                    //初次触发不能结束
                    if (_this.isOneTargger){
                        _this.isOneTargger = false;
                        return;
                    }
                    //判断是否点击到了控制点
                    let ent = commonMethod.judgeClickEntity(_this.curViewer,event.position);
                    if (!ent){
                        return;
                    }
                    //点击到哪个控制点
                    let indexOf = commonMethod.findElementFromArray(_this.tempControlPoint,n=>n.id == ent.id);
                    if (!indexOf){
                        return;
                    }
                    //判断是确定控制点还是修改控制点完成
                    if (!_this.pickContrlPoint){
                        _this.pickContrlPoint = ent;
                    }else {
                        //是完成一个控制点的修改
                        _this.pickContrlPoint = undefined;
                    }
                    return;
                }
                //线标的实现
                let num =  _this.positions.length;
                if (num < 1){
                    _this.positions.push(cartesian);
                    _this.createTempPlotFrankArrow();
                }else{
                    if (_this.positions.length >= 2) {
                        let marker = _this.createPlotFrankArrowMarker(_this.positions);
                        if (_this.finishCallback){
                            _this.finishCallback(marker);
                        }

                        _this.positions = [];
                        _this.curViewer.entities.remove(_this.tempEntity);
                        _this.tempEntity = null;
                        for (let tcp of _this.tempControlPoint){
                            _this.curViewer.entities.remove(tcp);
                        }
                        _this.tempControlPoint = [];
                        return;
                    }
                    _this.positions.pop();
                    _this.positions.push(cartesian);
                }
                _this.firstPos = cartesian;
                let cp = _this.createControlPoint(cartesian,true);
                _this.tempControlPoint.push(cp);
            }, ScreenSpaceEventType.LEFT_CLICK);

            _this.drawHandler.setInputAction(event => {

                if (!_this.isActive) {
                    return;
                }

                _this.destoryRightMenu();
                let judgeClickPosition = commonMethod.judgeClickPosition(event.endPosition,_this.curViewer);
                if (!judgeClickPosition) {
                    return;
                }
                let cartesian = _this.scene.globe.pick(judgeClickPosition.ray, _this.scene);

                if (!defined(cartesian)) {
                    return;
                }
                if (_this.isEdit) {
                    //通过控制点，修改标绘的点的位置
                    if (!_this.pickContrlPoint){
                        return;
                    }
                    //对于控制点的移动，改变点，变换箭头
                    let pos = _this.scene.globe.pick(judgeClickPosition.ray,_this.scene);
                    _this.pickContrlPoint.position.setValue(pos);

                    let indexOf = _this.tempControlPoint.indexOf(_this.pickContrlPoint);
                    _this.positions[indexOf] = pos;
                    return;
                }
                if (_this.positions.length <1){
                    return;
                }
                if (!_this.tempEntity){
                    return;
                }
                if (!_this.firstPos){
                    return;
                }
                let num = _this.positions.indexOf(_this.firstPos);
                //对应的移动的点
                _this.positions[num+1] = _this.scene.globe.pick(judgeClickPosition.ray, _this.scene);
            }, ScreenSpaceEventType.MOUSE_MOVE);

            _this.drawHandler.setInputAction(event => {

                if (!_this.isActive) {
                    return;
                }

                // if (_this.isOneTargger){
                //     _this.isOneTargger = false;
                //     return;
                // }

                if (_this.isEdit){
                    //改变标志
                    _this.isEdit = false;
                    _this.monitorManager.deployDefaultMonitor();

                    const markerManager = _this.monitorManager.getMarkerManager();
                    //将线的变化和标绘的position进行解绑
                    let pickArrow = markerManager.getMarkerById(_this.pickEntity.id);
                    pickArrow.setPositions(_this.positions);
                    pickArrow.setControlPointShow(false);

                    pickArrow.getEntity().polygon.hierarchy = _this.getDynamicHierarchy(_this.positions);
                    pickArrow.getEntity().polyline.positions = _this.getOutlinePos(_this.positions);

                    _this.pickEntity = undefined;
                    _this.pickContrlPoint = undefined;
                    _this.positions = [];
                    _this.tempControlPoint = [];
                    return;
                }

                let judgeClickPosition = commonMethod.judgeClickPosition(event.position,_this.curViewer);
                if (!judgeClickPosition) {
                    return;
                }

                if (_this.positions.length !== 2){
                   // CommonMethod.showMsg("双箭头由两个点组成！");
                    return;
                }

                //表示直接结束标绘
                let marker = _this.createPlotFrankArrowMarker(_this.positions);
                if (_this.finishCallback){
                    _this.finishCallback(marker);
                }

                _this.positions = [];
                _this.curViewer.entities.remove(_this.tempEntity);
                _this.tempEntity = null;
                for (let tcp of _this.tempControlPoint){
                    _this.curViewer.entities.remove(tcp);
                }
                _this.tempControlPoint = [];
            }, ScreenSpaceEventType.RIGHT_CLICK);
        }
    }
    createControlPoint(pos,bool){
        let _this = this;
        return _this.curViewer.entities.add({
            position:pos,
            point:{
                color : Color.WHITE,
                pixelSize : 5,
                heightReference: HeightReference.CLAMP_TO_GROUND
            },
            show:bool
        });
    }

    createTempPlotFrankArrow(){
        let _this = this;
        if (_this.material == null) {
            _this.material = Color.fromCssColorString('#ff0').withAlpha(0.5);
        }
        if (_this.outlineMaterial == null) {
            _this.outlineMaterial = new PolylineDashMaterialProperty({
                dashLength: 16,
                color: Color.fromCssColorString('#f00').withAlpha(0.7)
            });
        }

        let dynamicHierarchy = new CallbackProperty(function () {
            if (_this.positions.length > 1) {
                let p1 = _this.positions[0];
                let p2 = _this.positions[1];
                if (_this._isSimpleXYZ(p1, p2)) {
                    return null;
                }
                let firstPoint = commonMethod.getLonLatWithViewer(p1,_this.curViewer);
                let endPoints = commonMethod.getLonLatWithViewer(p2,_this.curViewer);
                let arrow = xp.algorithm.frankArrow([firstPoint.lon, firstPoint.lat], [endPoints.lon, endPoints.lat]);
                return new PolygonHierarchy(arrow);
            } else {
                return null;
            }
        }, false);
        let outlineDynamicPositions = new CallbackProperty(function () {
            if (_this.positions.length < 2) {
                return null;
            }
            let p1 = _this.positions[0];
            let p2 = _this.positions[1];
            if (_this._isSimpleXYZ(p1, p2)) {
                return null;
            }
            let firstPoint = commonMethod.getLonLatWithViewer(p1,_this.curViewer);
            let endPoints = commonMethod.getLonLatWithViewer(p2,_this.curViewer);
            let arrow = xp.algorithm.frankArrow([firstPoint.lon, firstPoint.lat], [endPoints.lon, endPoints.lat]);
            arrow.push(arrow[0]);
            return arrow;
        }, false);
        let bData = {
            polygon: new PolygonGraphics({
                hierarchy: dynamicHierarchy,
                material: _this.material,
                show: _this.fill
            }),
            polyline: {
                positions: outlineDynamicPositions,
                clampToGround: true,
                width: _this.outlineWidth,
                material: _this.outlineMaterial,
                show: _this.outline
            }
        };
        bData.polygon.extrudedHeight = undefined;
        if (_this.extrudedHeight > 0) {
            bData.polygon.extrudedHeight = undefined;
            bData.polygon.extrudedHeightReference = HeightReference.RELATIVE_TO_GROUND;
            bData.polygon.closeTop = true;
            bData.polygon.closeBottom = true;
        }
        _this.tempEntity = _this.curViewer.entities.add(bData);
    }

    _isSimpleXYZ(p1, p2) {
        return p1.x === p2.x && p1.y === p2.y && p1.z === p2.z;
    }

    getDynamicHierarchy(poss){
        let _this = this;
        if (poss.length > 1) {
            let p1 = poss[0];
            let p2 = poss[1];
            if (_this._isSimpleXYZ(p1, p2)) {
                return null;
            }
            let firstPoint = commonMethod.getLonLatWithViewer(p1,_this.curViewer);
            let endPoints = commonMethod.getLonLatWithViewer(p2,_this.curViewer);
            let arrow = xp.algorithm.frankArrow([firstPoint.lon, firstPoint.lat], [endPoints.lon, endPoints.lat]);
            return new PolygonHierarchy(arrow);
        } else {
            return null;
        }
    }
    getOutlinePos(poss) {
        let _this = this;
        if (poss.length < 2) {
            return null;
        }
        let p1 = poss[0];
        let p2 = poss[1];
        if (_this._isSimpleXYZ(p1, p2)) {
            return null;
        }
        let firstPoint = commonMethod.getLonLatWithViewer(p1,_this.curViewer);
        let endPoints = commonMethod.getLonLatWithViewer(p2,_this.curViewer);
        let arrow = xp.algorithm.frankArrow([firstPoint.lon, firstPoint.lat], [endPoints.lon, endPoints.lat]);
        arrow.push(arrow[0]);
        return arrow;
    }
    createPlotFrankArrowMarker(positions){
        let _this = this;
        //生成的军标类型不同
        let markerId = _this.params.markerId?_this.params.markerId:commonMethod.creatUUID();
        let markerCode = _this.params.markerCode;

        if (_this.material == null) {
            _this.material = Color.RED.withAlpha(0.5);
        }
        if (_this.params.camp){
            let camp = _this.params.camp;
            if (camp === commonConstants.troopCamp.our_enemy){
                _this.material = Cesium.Color.BLUE.withAlpha(0.3);
            }else if (camp === commonConstants.troopCamp.our_friend){
                _this.material = Cesium.Color.PINK.withAlpha(0.3);
            }
        }
        let color = Color.RED;
        if (_this.params.camp){
            let camp = _this.params.camp;
            if (camp === commonConstants.troopCamp.our_enemy){
                color = Cesium.Color.BLUE;
            }else if (camp === commonConstants.troopCamp.our_friend){
                color = Cesium.Color.PINK;
            }
        }
        _this.outlineMaterial = new PolylineDashMaterialProperty({
            dashLength: 16,
            color: color.withAlpha(0.7)
        });


        let dynamicHierarchy = _this.getDynamicHierarchy(positions);
        let outlineDynamicPositions = _this.getOutlinePos(positions);

        let controllerPoints = [];
        //控制点的生成
        for (let i = 0; i < positions.length; i++) {
            let pos = positions[i];
            let contrlPos = _this.createControlPoint(pos,false);
            controllerPoints.push(contrlPos);
        }

        let bData = {
            id:markerId,
            polygon: new PolygonGraphics({
                hierarchy: dynamicHierarchy,
                material: _this.material,
                show: _this.fill
            }),
            polyline: {
                positions: outlineDynamicPositions,
                clampToGround: true,
                width: _this.outlineWidth,
                material: _this.outlineMaterial,
                show: _this.outline
            }
        };
        bData.polygon.extrudedHeight = undefined;
        if (_this.extrudedHeight > 0) {
            bData.polygon.extrudedHeight = undefined;
            bData.polygon.extrudedHeightReference = HeightReference.RELATIVE_TO_GROUND;
            bData.polygon.closeTop = true;
            bData.polygon.closeBottom = true;
        }
        let entity = _this.curViewer.entities.add(bData);
        entity.monitorKey = _this.monitorKey;
        entity.entityType = commonConstants.entityType.marker;

        _this.monitorManager.getDefaultMonitor().putEntity(entity);

        const marker = new Marker({
            markerId:markerId,
            isCompositeEntity:false,
            markerName:_this.params.markerName,
            entity:entity,
            position:positions,
            markerCode:markerCode
        });

        marker.setControlPoints(controllerPoints);
        return marker;
    }
    destoryRightMenu(){
        let _this = this;
        if (_this.rightmenu){
            _this.rightmenu.destroy();
            _this.rightmenu = null;
            _this.monitorManager.getDefaultMonitor().setCurRigthMenu(_this.rightmenu);
        }
    }
    showRightMenuEntity (event,entity) {
        let _this = this;
        _this.destoryRightMenu();
        _this.rightmenu = new RightMenu([{
            itemName: "删除",
            icon: "layui-icon layui-icon-delete",
            callback: function () {
                //表示删除点标标绘
            
                const markerManager = _this.monitorManager.getMarkerManager();
                //删除点标之前判断是否存在
                const markerById = markerManager.getMarkerById(entity.id);
                if (markerById){
                    //删除标绘
                    markerManager.removeMarker(markerById);
                }
                //关闭标绘
                _this.monitorManager.deployDefaultMonitor();
            }
        }],_this.curViewer);
        _this.rightmenu.show(event,true);
        _this.monitorManager.getDefaultMonitor().setCurRigthMenu(_this.rightmenu);
    }
    /**
     * 展示属性面板
     * @param entity
     */
    showAttrPane(entity){
    }
    /**
     * 处理默认监听带来的事件
     */
    handerEvent (event,pickEntity,clickType) {
        let _this = this;
        _this.pickEntity = pickEntity;
        if (clickType === ScreenSpaceEventType.RIGHT_CLICK){
            //右键菜单
           _this.showRightMenuEntity(event,pickEntity);
        }else if (clickType === ScreenSpaceEventType.LEFT_CLICK){
            _this.isEdit = true;
            _this.isActive = true;
            //只为了触发时间和监听对象的转变，不能直接就进入结束状态
           _this.isOneTargger = true;

            //将线的变化和标绘的position进行绑定
            const markerManager = _this.monitorManager.getMarkerManager();
            let pickArrow = markerManager.getMarkerById(_this.pickEntity.id);
            _this.positions = pickArrow.getPositions();
            _this.tempControlPoint = pickArrow.getControlPoints();
            pickArrow.setControlPointShow(true);

            pickArrow.getEntity().polygon.hierarchy = new CallbackProperty(()=>{
                return _this.getDynamicHierarchy(_this.positions);
            },false);

            pickArrow.getEntity().polyline.positions = new CallbackProperty(()=>{
                return _this.getOutlinePos(_this.positions);
            },false);
        }
    }
    reMarker(positions,callbackFunction){
        let _this = this;
        const marker = _this.createPlotFrankArrowMarker(positions);
        if (callbackFunction){
            callbackFunction(marker);
        }
    }

}

/**
 * 圆
 */
export class GlobeCircleMarker{
    constructor(v){
        this.curViewer = v;
        this.scene = v.scene;
        this.clock = v.clock;
        this.canvas = v.scene.canvas;
        this.camera = v.scene.camera;
        this.ellipsoid = v.scene.globe.ellipsoid;

        this.material = Color.fromCssColorString('#f00').withAlpha(0.3);
        this.radiusLineMaterial = new PolylineDashMaterialProperty({
            dashLength: 16,
            color: Color.fromCssColorString('#f00').withAlpha(0.7)
        });
        this.outlineWidth = 2;
        this.fill = true;
        this.outline = true;
        this.tempControlPoint = [];

        this.pickEntity = undefined;
        this.pickContrlPoint = undefined;
        this.positions = undefined;
        this.drawHandler = undefined;
        this.params = undefined;
        this.tempEntity = undefined;
        this.finishCallback = undefined;
        this.monitorManager = undefined;
        this.firstPos = undefined;
    
        this.isOneTargger = false;
        this.monitorKey =  commonConstants.monitorKey.line.globeCircle,
        this.rightmenu = undefined;
    
        this.isActive = false;
        this.isEdit = false;
    }

    setParams(params){
        let _this = this;
        _this.params = params;
    }
    setFinishCallback(callbackFunction){
        let _this = this;
        _this.finishCallback = callbackFunction;
    }
    /**
     * 
     * @param {MonitorManager} manager 
     */
    setManager(manager){
        const _this = this;
        _this.monitorManager = manager;
    }

    setActive(bool){
        let _this = this;
        _this.isActive = bool;
    }
    /**
     * 这是开始标绘普通航迹
     */
    deploy() {
        let _this = this;

        if (!_this.drawHandler) {
            _this.positions = [];
            _this.firstPos = undefined;
            _this.drawHandler = new ScreenSpaceEventHandler(_this.canvas);

            _this.drawHandler.setInputAction(function (event) {

                if (!_this.isActive) {
                    return;
                }

                let p = commonMethod.judgeClickPosition(event.position,_this.curViewer);
                if (!p) {
                    return;
                }

                _this.destoryRightMenu();
                let cartesian = _this.scene.globe.pick(p.ray, _this.scene);

                if (!defined(cartesian)) {
                    return;
                }

                if (_this.isEdit) {
                    //初次触发不能结束
                    if (_this.isOneTargger){
                        _this.isOneTargger = false;
                        return;
                    }
                    //判断是否点击到了控制点
                    let ent = commonMethod.judgeClickEntity(_this.curViewer,event.position);
                    if (!ent){
                        return;
                    }
                    //点击到哪个控制点
                    let indexOf = commonMethod.findElementFromArray(_this.tempControlPoint,n=>n.id == ent.id);
                    if (!indexOf){
                        return;
                    }
                    //判断是确定控制点还是修改控制点完成
                    if (!_this.pickContrlPoint){
                        _this.pickContrlPoint = indexOf;
                    }else {
                        //是完成一个控制点的修改
                        _this.pickContrlPoint = undefined;
                    }
                    return;
                }
                //线标的实现
                let num =  _this.positions.length;
                if (num < 1){
                    _this.positions.push(cartesian);
                    _this.createTempGlobeCircle();
                }else{
                    if (_this.positions.length >= 2) {
                        let marker = _this.createGlobeCircleMarker(_this.positions);
                        if (_this.finishCallback){
                            _this.finishCallback(marker);
                        }

                        _this.positions = [];
                        _this.curViewer.entities.remove(_this.tempEntity);
                        _this.tempEntity = null;
                        for (let tcp of _this.tempControlPoint){
                            _this.curViewer.entities.remove(tcp);
                        }
                        _this.tempControlPoint = [];
                        return;
                    }
                    _this.positions.pop();
                    _this.positions.push(cartesian);
                }
                _this.firstPos = cartesian;
                let cp = _this.createControlPoint(cartesian,true);
                _this.tempControlPoint.push(cp);
            }, ScreenSpaceEventType.LEFT_CLICK);

            _this.drawHandler.setInputAction(function (event) {

                if (!_this.isActive) {
                    return;
                }

                _this.destoryRightMenu();
                let judgeClickPosition = commonMethod.judgeClickPosition(event.endPosition,_this.curViewer);
                if (!judgeClickPosition) {
                    return;
                }
                let cartesian = _this.scene.globe.pick(judgeClickPosition.ray, _this.scene);

                if (!defined(cartesian)) {
                    return;
                }
                if (_this.isEdit) {
                    //通过控制点，修改标绘的点的位置
                    if (!_this.pickContrlPoint){
                        return;
                    }
                    //对于控制点的移动，改变点，变换箭头
                    let pos = _this.scene.globe.pick(judgeClickPosition.ray,_this.scene);
                    _this.pickContrlPoint.position.setValue(pos);

                    let indexOf = _this.tempControlPoint.indexOf(_this.pickContrlPoint);
                    _this.positions[indexOf] = pos;
                    return;
                }
                if (_this.positions.length <1){
                    return;
                }
                if (!_this.tempEntity){
                    return;
                }
                if (!_this.firstPos){
                    return;
                }
                let num = _this.positions.indexOf(_this.firstPos);
                //对应的移动的点
                _this.positions[num+1] = _this.scene.globe.pick(judgeClickPosition.ray, _this.scene);
            }, ScreenSpaceEventType.MOUSE_MOVE);

            _this.drawHandler.setInputAction(function (event) {

                if (!_this.isActive) {
                    return;
                }

                if (_this.isOneTargger){
                    _this.isOneTargger = false;
                    return;
                }

                if (_this.isEdit){
                    //改变标志
                    _this.isEdit = false;
                    _this.monitorManager.deployDefaultMonitor();

                    const markerManager = _this.monitorManager.getMarkerManager();
                    //将线的变化和标绘的position进行解绑
                    let pickArrow = markerManager.getMarkerById(_this.pickEntity.id);
                    pickArrow.setPositions(_this.positions);
                    pickArrow.setControlPointShow(false);

                    pickArrow.getEntity().polygon.hierarchy = _this.getDynamicHierarchy(_this.positions);
                    pickArrow.getEntity().position = _this.getOutlinePos(_this.positions);

                    _this.pickEntity = undefined;
                    _this.pickContrlPoint = undefined;
                    _this.positions = [];
                    _this.tempControlPoint = [];
                    return;
                }

                let judgeClickPosition = commonMethod.judgeClickPosition(event.position,_this.curViewer);
                if (!judgeClickPosition) {
                    return;
                }

                if (_this.positions.length !== 2){
                    // CommonMethod.showMsg("圆由两个点组成！");
                    return;
                }

                //表示直接结束标绘
                let marker = _this.createGlobeCircleMarker(_this.positions);
                if (_this.finishCallback){
                    _this.finishCallback(marker);
                }

                _this.positions = [];
                _this.curViewer.entities.remove(_this.tempEntity);
                _this.tempEntity = null;
                for (let tcp of _this.tempControlPoint){
                    _this.curViewer.entities.remove(tcp);
                }
                _this.tempControlPoint = [];
            }, ScreenSpaceEventType.RIGHT_CLICK);
        }
    }
    createControlPoint(pos,bool){
        let _this = this;
        return _this.curViewer.entities.add({
            position:pos,
            point:{
                color : Color.WHITE,
                pixelSize : 5,
                heightReference: HeightReference.CLAMP_TO_GROUND
            },
            show:bool
        });
    }
    createTempGlobeCircle(){
        let _this = this;
        if (_this.material == null) {
            _this.material = Color.fromCssColorString('#f00').withAlpha(0.3);
        }

        if (_this.radiusLineMaterial == null) {
            _this.radiusLineMaterial = new PolylineDashMaterialProperty({
                dashLength: 16,
                color: Color.fromCssColorString('#f00').withAlpha(0.7)
            });
        }

        const dynamicHierarchy = new CallbackProperty(function () {
            if (_this.positions.length > 1) {
                let dis = _this._computeCircleRadius3D(_this.positions);
                let disString = (dis / 1000).toFixed(3);
                _this.tempEntity.label.text = disString + "km";
                let pnts = _this._computeCirclePolygon(_this.positions);
                console.log(pnts.length);
                
                if(!pnts){
                    return [];
                }
                return pnts;
            } else {
                return [];
            }
        }, false);

        let lineDynamicPositions = new CallbackProperty(function () {
            if (_this.positions.length > 1) {
                return _this.positions;
            } else {
                return [];
            }
        }, false);

        let labelDynamicPosition = new CallbackProperty(function () {
            if (_this.positions.length > 1) {
                let p1 = _this.positions[0];
                let p2 = _this.positions[1];
                return _this._computeCenterPotition(p1, p2);
            } else {
                return [];
            }
        }, false);

        let bData = {
            position: labelDynamicPosition,
            label: {
                text: "",
                font: '14px Helvetica',
                fillColor: Color.SKYBLUE,
                outlineColor: Color.BLACK,
                outlineWidth: 1,
                style: LabelStyle.FILL_AND_OUTLINE,
                eyeOffset: new ConstantProperty(new Cartesian3(0, 0, -9000)),
                pixelOffset: new Cartesian2(16, 16)
            },
            polygon: {
                // hierarchy: dynamicHierarchy,
                positions: dynamicHierarchy,
                material: _this.material,
                height:1,
                fill: true,
                outline: true,
                outlineWidth: 3,
                outlineColor:Color.BLACK
            },
            polyline: {
                positions: lineDynamicPositions,
                clampToGround: true,
                width: 1.5,
                material: _this.radiusLineMaterial
            }
        };
        bData.polygon.extrudedHeight = undefined;
        if (_this.extrudedHeight > 0) {
            bData.polygon.extrudedHeight = undefined;
            bData.polygon.extrudedHeightReference = HeightReference.RELATIVE_TO_GROUND;
            bData.polygon.closeTop = true;
            bData.polygon.closeBottom = true;
        }
        _this.tempEntity = _this.curViewer.entities.add(bData);
    }
    _computeCircleRadius3D(positions) {
        let c1 = positions[0];
        let c2 = positions[1];
        let x = Math.pow(c1.x - c2.x, 2);
        let y = Math.pow(c1.y - c2.y, 2);
        let z = Math.pow(c1.z - c2.z, 2);
        return Math.sqrt(x + y + z);
    }
    _computeCirclePolygon(positions) {
        let _this = this;

        try {
            if (!positions || positions.length < 2) {
                return null;
            }
            let cp = positions[0];
            let r = _this._computeCircleRadius3D(positions);
            return _this._computeCirclePolygon2(cp, r);
        } catch (err) {
            return [];
        }
    }
    _computeCirclePolygon2(center, radius) {
        try {
            if (!center || radius <= 0) {
                return null;
            }
            let cep = EllipseGeometryLibrary.computeEllipsePositions({
                center: center,
                semiMajorAxis: radius,
                semiMinorAxis: radius,
                rotation: 0,
                granularity: 0.005
            }, false, true);
            if (!cep || !cep.outerPositions) {
                return null;
            }
            let pants = Cartesian3.unpackArray(cep.outerPositions);
            pants[pants.length] = pants[0];
            return pants;
           
        } catch (err) {
            console.log(err);    
            return null;
        }
    }
    _computeCenterPotition(p1, p2) {
        let _this = this;
        let c1 = _this.ellipsoid.cartesianToCartographic(p1);
        let c2 = _this.ellipsoid.cartesianToCartographic(p2);
        let cm = new EllipsoidGeodesic(c1, c2).interpolateUsingFraction(0.5);
        return _this.ellipsoid.cartographicToCartesian(cm);
    }
    _isSimpleXYZ(p1, p2) {
        return p1.x === p2.x && p1.y === p2.y && p1.z === p2.z;
    }
    getDynamicHierarchy(poss){
        let _this = this;
        if (poss.length > 1) {
            let pants = _this._computeCirclePolygon(poss);
            return new PolygonHierarchy(pants);
        } else {
            return null;
        }
    }
    getOutlinePos(poss) {
        let _this = this;
        if (poss.length > 1) {
            let p1 = poss[0];
            let p2 = poss[1];
            return _this._computeCenterPotition(p1, p2);
        } else {
            return null;
        }
    }
    createGlobeCircleMarker(positions){
        let _this = this;
        //生成的军标类型不同
        let markerId = _this.params.markerId?_this.params.markerId:commonMethod.creatUUID();
        let markerCode = _this.params.markerCode;

        if (_this.material == null) {
            _this.material = Color.RED.withAlpha(0.3);
        }
        if (_this.params.camp){
            let camp = _this.params.camp;
            if (camp === commonConstants.troopCamp.our_enemy){
                _this.material = Cesium.Color.BLUE.withAlpha(0.3);
            }else if (camp === commonConstants.troopCamp.our_friend){
                _this.material = Cesium.Color.PINK.withAlpha(0.3);
            }
        }

        let color = Color.RED;
        if (_this.params.camp){
            let camp = _this.params.camp;
            if (camp === commonConstants.troopCamp.our_enemy){
                color = Cesium.Color.BLUE;
            }else if (camp === commonConstants.troopCamp.our_friend){
                color = Cesium.Color.PINK;
            }
        }
        _this.radiusLineMaterial = new PolylineDashMaterialProperty({
            dashLength: 16,
            color: color.withAlpha(0.7)
        });


        let dynamicHierarchy = _this.getDynamicHierarchy(positions);
        let lineDynamicPositions = positions;
        let labelDynamicPosition = _this.getOutlinePos(positions);

        let dis = _this._computeCircleRadius3D(positions);
        dis = (dis / 1000).toFixed(3);
        let disLable = dis + "km";

        let controllerPoints = [];
        //控制点的生成
        for (let i = 0; i < positions.length; i++) {
            let pos = positions[i];
            let contrlPos = _this.createControlPoint(pos,false);
            controllerPoints.push(contrlPos);
        }

        let bData = {
            id:markerId,
            position: labelDynamicPosition,
            label: {
                text: disLable,
                font: '14px Helvetica',
                fillColor: Color.SKYBLUE,
                outlineColor: Color.BLACK,
                outlineWidth: 1,
                style: LabelStyle.FILL_AND_OUTLINE,
                eyeOffset: new ConstantProperty(new Cartesian3(0, 0, -9000)),
                pixelOffset: new Cartesian2(16, 16)
            },
            polygon: new PolygonGraphics({
                hierarchy: dynamicHierarchy,
                material: _this.material,
                fill: true,
                outline: true,
                outlineWidth: 3,
                outlineColor:Color.BLACK
            }),
            polyline: {
                positions: lineDynamicPositions,
                clampToGround: true,
                width: 1.5,
                material: _this.radiusLineMaterial
            }
        };
        bData.polygon.extrudedHeight = undefined;
        if (_this.extrudedHeight > 0) {
            bData.polygon.extrudedHeight = undefined;
            bData.polygon.extrudedHeightReference = HeightReference.RELATIVE_TO_GROUND;
            bData.polygon.closeTop = true;
            bData.polygon.closeBottom = true;
        }
        let entity = _this.curViewer.entities.add(bData);
        entity.monitorKey = _this.monitorKey;
        entity.entityType = commonConstants.entityType.marker;

        _this.monitorManager.getDefaultMonitor().putEntity(entity);

        const marker = new Marker({
            markerId:markerId,
            isCompositeEntity:false,
            markerName:_this.params.markerName,
            entity:entity,
            position:positions,
            markerCode:markerCode
        });

        marker.setControlPoints(controllerPoints);
        return marker;
    }
    destoryRightMenu(){
        let _this = this;
        if (_this.rightmenu){
            _this.rightmenu.destroy();
            _this.rightmenu = null;
            _this.monitorManager.getDefaultMonitor().setCurRigthMenu(_this.rightmenu);
        }
    }
    showRightMenuEntity(event,entity) {
        let _this = this;
        _this.destoryRightMenu();
        _this.rightmenu = new RightMenu([{
            itemName: "删除",
            icon: "layui-icon layui-icon-delete",
            callback: function () {
                //表示删除点标标绘
                const markerManager = _this.monitorManager.getMarkerManager();
                //删除点标之前判断是否存在
                const markerById = markerManager.getMarkerById(entity.id);
                if (markerById){
                    //删除标绘
                    markerManager.removeMarker(markerById);
                }
                //关闭标绘
                _this.monitorManager.deployDefaultMonitor();
            }
        }],_this.curViewer);
        _this.rightmenu.show(event,true);
        _this.monitorManager.getDefaultMonitor().setCurRigthMenu(_this.rightmenu);
    }
    /**
     * 展示属性面板
     * @param entity
     */
    showAttrPane(entity){
    }
    /**
     * 处理默认监听带来的事件
     */
    handerEvent (event,pickEntity,clickType) {
        let _this = this;
        _this.pickEntity = pickEntity;
        if (clickType === ScreenSpaceEventType.RIGHT_CLICK){
            //右键菜单
            _this.showRightMenuEntity(event,pickEntity);
        }else if (clickType === ScreenSpaceEventType.LEFT_CLICK){
            _this.isEdit = true;
            _this.isActive = true;
            //只为了触发时间和监听对象的转变，不能直接就进入结束状态
            _this.isOneTargger = true;

            //将线的变化和标绘的position进行绑定
            const markerManager = _this.monitorManager.getMarkerManager();
            let pickArrow = markerManager.getMarkerById(_this.pickEntity.id);
            _this.positions = pickArrow.getPositions();
            _this.tempControlPoint = pickArrow.getControlPoints();
            pickArrow.setControlPointShow(true);

            pickArrow.getEntity().polygon.hierarchy = new CallbackProperty(function (){
                let dis = _this._computeCircleRadius3D(_this.positions);
                dis = (dis / 1000).toFixed(3);
                pickArrow.getEntity().label.text = dis + "km";

                return _this.getDynamicHierarchy(_this.positions);
            },false);

            pickArrow.getEntity().position = new CallbackProperty(function (){
                return _this.getOutlinePos(_this.positions);
            },false);

            pickArrow.getEntity().polyline.positions =  new CallbackProperty(function () {
                if (_this.positions.length > 1) {
                    return _this.positions;
                } else {
                    return [];
                }
            }, false); 
        }
    }
    reMarker(positions,callbackFunction){
        let _this = this;
        const marker = _this.createGlobeCircleMarker(positions);
        if (callbackFunction){
            callbackFunction(marker);
        }
    }
}
